package com.neulogistics.contrib.warehouse.api.application.rpc;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.neulogistics.contrib.warehouse.api.application.command.PurchaseCommand;
import com.neulogistics.contrib.warehouse.api.application.rpc.dto.GetSourceWarehouseIdCommand;
import com.neulogistics.contrib.warehouse.domain.model.aggregate.CirculationOrder;
import com.neulogistics.contrib.warehouse.domain.repository.ICirculationOrderRepository;
import com.neulogistics.infrastructure.common.rpc.client.ProcuredCenterClient;
import com.neulogistics.infrastructure.common.rpc.client.TransferCenterClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WarehouseRPC implements IWarehouseRPC {
    @Resource
    TransferCenterClient transferCenterClient;

    @Resource
    ProcuredCenterClient procuredCenterClient;

    @Resource
    ICirculationOrderRepository circulationOrderRepository;

    @Override
    public Long getOrderIdByTransfer(Long transferOrderId) {
        Long orderId = transferCenterClient.getOrderIdByTransfer(transferOrderId).getData();
        return orderId;
    }

    @Override
    public Boolean purchase(PurchaseCommand purchaseCommand) {
        Boolean result = procuredCenterClient.purchase(purchaseCommand).getData();
        return result == null ? Boolean.FALSE : result;
    }

    @Override
    public Integer getSourceWarehouseId(Long circulationId, Integer target) {
        CirculationOrder circulationOrder = circulationOrderRepository.findById(circulationId);
        GetSourceWarehouseIdCommand command = new GetSourceWarehouseIdCommand();
        command.setTransferId(circulationOrder.getTransferId());
        command.setTarget(target);
        Integer warehouseId = transferCenterClient.getSourceWarehouseId(command).getData();
        return warehouseId;
    }

}
